package com.crm.business.crm.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.business.admin.service.AdminService;
import com.crm.business.crm.mapper.CrmDeptFieldMapper;
import com.crm.business.crm.service.IBillCrmTrainingService;
import com.crm.business.crm.service.ICrmDeptFieldService;
import com.crm.business.crm.service.ICrmFieldService;
import com.crm.business.crm.service.ICrmRoleFieldService;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.business.utils.UserUtil;
import com.crm.common.crm.entity.PO.CrmDeptField;
import com.crm.common.crm.entity.PO.CrmField;
import com.crm.common.crm.entity.PO.CrmRoleField;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.feign.admin.entity.SimpleDept;
import com.crm.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色字段授权表 服务实现类
 * </p>
 *
 */
@Service
public class CrmDeptFieldServiceImpl extends BaseServiceImpl<CrmDeptFieldMapper, CrmDeptField> implements ICrmDeptFieldService {

    @Autowired
    private AdminService adminService;

    @Autowired
    private ICrmFieldService crmFieldService;

    @Autowired
    private IBillCrmTrainingService crmTrainingService;

    /**
     * 查询用户所属字段权限
     *
     * @param label 类型
     * @return data
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CrmDeptField> queryUserFieldAuth(Integer label, Integer authLevel) {
        List<CrmDeptField> list = queryUserDeptField(label);
        list.removeIf(crmRoleField -> crmRoleField.getAuthLevel() < authLevel);
        return list;
    }

    private List<CrmDeptField> queryUserDeptField(Integer label) {
        QueryWrapper<CrmDeptField> queryWrapper = new QueryWrapper<>();
        Integer deptId = UserUtil.getUser().getDeptId();
        queryWrapper.select("field_id", "field_name", "max(auth_level) as auth_level", "field_type")
                .eq("dept_id", deptId)
                .eq("label", label)
                .groupBy("field_name");
        List<CrmDeptField> list = list(queryWrapper);
        if (list.size() == 0) {
            if(UserUtil.isAdmin()){
                return list;
            }
            queryDeptField(deptId, label);
            list = list(queryWrapper);
        }
        return list;
    }

    /**
     * 查询没有权限的字段
     *
     * @param label label
     * @return data
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> queryNoAuthField(Integer label) {
        List<CrmDeptField> list = queryUserDeptField(label);
        list.removeIf(crmRoleField -> crmRoleField.getAuthLevel() > 1);
        List<String> collect = crmFieldService.lambdaQuery().select(CrmField::getFieldName).eq(CrmField::getIsHidden, 1).eq(CrmField::getLabel, label).list()
                .stream().map(field -> StrUtil.toCamelCase(field.getFieldName())).collect(Collectors.toList());
        collect.addAll(list.stream().map(field -> StrUtil.toCamelCase(field.getFieldName())).collect(Collectors.toList()));
        return collect;
    }

    /**
     * 查询字段授权设置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CrmDeptField> queryDeptField(Integer deptId, Integer label) {
        List<CrmDeptField> fields = lambdaQuery().eq(CrmDeptField::getDeptId, deptId).eq(CrmDeptField::getLabel, label).list();
        //需要初始化
        if (fields.size() == 0) {
            List<CrmField> list = SpringUtils.getBean(ICrmFieldService.class).list(label, true);
            list.forEach(crmField -> {
                CrmDeptField deptField = new CrmDeptField();
                deptField.setDeptId(deptId);
                deptField.setLabel(label);
                deptField.setFieldId(crmField.getFieldId());
                deptField.setFieldName(crmField.getFieldName());
                deptField.setName(crmField.getName());
                deptField.setAuthLevel(3);
                switch (label) {
                    case 1: {
                        if ("leads_name".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else if ("next_time".equals(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 2: {
                        if ("customer_name".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else if ("next_time".equals(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 3: {
                        if ("customer_id".equals(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else if ("next_time".equals(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 4: {
                        if (Arrays.asList("name", "unit", "price", "category_id").contains(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 5: {
                        if ("customer_id".equals(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 6: {
                        if ("num".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 7: {
                        deptField.setOperateType(1);
                        break;
                    }
                    case 17: {
                        if (Arrays.asList("owner_user_id", "customer_id", "contract_id").contains(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 19: {
                        if ("customer_name".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else if ("next_time".equals(crmField.getFieldName())) {
                            deptField.setOperateType(4);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 25: {
                        if ("number".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 24: {
                        if ("number".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                    case 28: {
                        if ("number".equals(crmField.getFieldName())) {
                            deptField.setOperateType(3);
                        } else {
                            deptField.setOperateType(1);
                        }
                        break;
                    }
                }

                deptField.setFieldType(crmField.getFieldType());
                fields.add(deptField);
            });
            switch (label) {
                case 1: {
                    fields.removeIf(item -> Arrays.asList("owner_user_name","last_content","create_user_name","create_time","update_time","last_time",
                            "serial_number","is_transform","vip_deal_status","vip_deal_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_content", "最后跟进记录", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_time", "最后跟进时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "serial_number", "系统编号", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "is_transform", "是否转化", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "vip_deal_status", "VIP成交状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "vip_deal_time", "VIP成交时间", 2, 4, 1));
                    break;
                }
                case 2: {
                    fields.removeIf(item -> Arrays.asList("owner_user_name","last_content","create_user_name","create_time","update_time","last_time",
                            "receive_time","leads_create_name","leads_user_name","assistant_user_name","leads_create_time",
                            "owner_dept_id","serial_number","draw_time","repayment_status","student_status","vip_deal_status","vip_deal_time","sign_class").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_content", "最后跟进记录", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_time", "最后跟进时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "receive_time", "负责人获取客户时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "deal_status", "成交状态", 2, 4, 1));
//                    fields.add(new CrmDeptField(label, deptId, "status", "锁定状态", 2, 2, 1));
//                    fields.add(new CrmDeptField(label, deptId, "pool_day", "距进入公海天数", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "leads_create_name", "线索创建人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "leads_user_name", "线索负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "assistant_user_name", "助教协作人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "leads_create_time", "线索创建时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_dept_id", "负责人所属部门", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "serial_number", "系统编号", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "draw_time", "领取时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "repayment_status", "还款状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "student_status", "学员状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "vip_deal_status", "成交状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "vip_deal_time", "成交时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "sign_class", "是否报课", 3, 1, 1));
                    break;
                }
                case 3: {
                    fields.removeIf(item -> Arrays.asList("owner_user_name","create_user_name","create_time","update_time","last_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_time", "最后跟进时间", 2, 2, 1));
                    break;
                }
                case 4: {
                    fields.removeIf(item -> Arrays.asList("status","owner_user_name","create_user_name","create_time","update_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "status", "是否上下架", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    break;
                }
                case 5: {
                    fields.removeIf(item -> Arrays.asList("type_name","status_name","owner_user_name","create_user_name","create_time","update_time","last_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "type_name", "商机阶段", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "status_name", "商机状态组", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_time", "最后跟进时间", 2, 2, 1));
                    break;
                }
                case 6: {
                    fields.removeIf(item -> Arrays.asList("owner_user_name","create_user_name","create_time","update_time","last_time","last_content",
                            "received_money","unreceived_money","check_status","repayment_status","drop_out_time","deal_time","deal_status",
                            "bill_return_type","bill_time","type","customer_id","money","preferential_money","check_time","return_type","org_name").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_time", "最后跟进时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_content", "最后跟进记录", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "received_money", "已收款金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "unreceived_money", "未收款金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "refund_monery", "退款金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "check_status", "审核状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "repayment_status", "还款状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "drop_out_time", "退学时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "deal_time", "成交时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "deal_status", "是否成交", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "bill_return_type", "账单支付方式", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "bill_time", "账单领取时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "type", "合同来源", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "customer_id", "学员", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "money", "成交价格", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "preferential_money", "优惠金额", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "check_time", "审核通过时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "return_type", "支付方式", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "org_name", "机构名称", 2, 4, 1));
                    break;
                }
                case 7: {
                    fields.removeIf(item -> Arrays.asList("customer_id","contract_id","contractMoney","money","owner_user_name","create_user_name",
                            "create_time","update_time","check_status","owner_order_pool","create_dept_id","type","periods_num","repay_periods",
                            "overdue_time","repayment_status","bill_return_type","check_time","return_type").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "customer_id", "学员", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "contract_id", "合同订单编号", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "contractMoney", "合同金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "money", "实际付款金额", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "check_status", "审核状态", 2, 4, 1));

                    fields.add(new CrmDeptField(label, deptId, "owner_order_pool", "所属账单池", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_dept_id", "创建人所在部门", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "type", "回款类别", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "periods_num", "期数", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "repay_periods", "第几期", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "overdue_time", "逾期时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "repayment_status", "还款状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "bill_return_type", "账单支付方式", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "check_time", "审核通过时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "return_type", "支付方式", 3, 4, 1));
                    break;
                }
                case 17: {
                    fields.removeIf(item -> Arrays.asList("create_user_name","create_time","update_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    break;
                }
                case 19: {
                    fields.removeIf(item -> Arrays.asList("owner_user_name","last_content","create_user_name","create_time","update_time","last_time",
                            "receive_time","leads_create_name","leads_user_name","assistant_user_name","leads_create_time",
                            "owner_dept_id","serial_number","draw_time","repayment_status","student_status","vip_deal_status","vip_deal_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "assistant_user_name", "助教协作人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_content", "最后跟进记录", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "last_time", "最后跟进时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "receive_time", "负责人获取客户时间", 2, 2, 1));
//                    fields.add(new CrmDeptField(label, deptId, "status", "锁定状态", 2, 2, 1));
//                    fields.add(new CrmDeptField(label, deptId, "pool_day", "距进入公海天数", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "leads_create_name", "线索创建人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "leads_user_name", "线索负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "leads_create_time", "线索创建时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_dept_id", "负责人所在部门", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "serial_number", "系统编号", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "draw_time", "领取时间", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "repayment_status", "还款状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "student_status", "学员状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "vip_deal_status", "成交状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "vip_deal_time", "成交时间", 2, 4, 1));
                    break;
                }
                case 25: {
                    fields.removeIf(item -> Arrays.asList("contract_money","money","owner_user_name","refund_user_name","create_user_name",
                            "create_time","update_time","check_status","student_id","contract_id","return_type").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "student_id", "VIP学员", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "contract_id", "合同订单编号", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "contractMoney", "合同金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "receivablesMoney", "回款金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "money", "退款金额", 3, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "refund_user_name", "退款负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "check_status", "审核状态", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "return_type", "支付方式", 3, 4, 1));
                    break;
                }
                case 24: {
                    fields.removeIf(item -> Arrays.asList("three_part_order_no","payment_time","overdue_money","overdue_status","owner_user_name","create_user_name","create_time","update_time").contains(item.getFieldName()));
                    fields.add(new CrmDeptField(label, deptId, "three_part_order_no", "三方平台合同订单号", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "payment_time", "付款时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "overdue_money", "逾期金额", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "overdue_status", "逾期状态", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "owner_user_name", "负责人", 2, 4, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_user_name", "创建人", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "create_time", "创建时间", 2, 2, 1));
                    fields.add(new CrmDeptField(label, deptId, "update_time", "更新时间", 2, 2, 1));
//                    fields.add(new CrmRoleField(label, deptId, "check_status", "审核状态", 2, 4, 1));
                    break;
                }
                default:
                    break;
            }

            Result result = crmTrainingService.queryTrainingStatus();

            if(result.hasSuccess()){
                if(ObjectUtil.equal(result.getData(),0)){
                    fields.removeIf(item -> Arrays.asList("training_periods", "teacher_id").contains(item.getFieldName()));
                }
            }

//            saveBatch(fields);
            for(CrmDeptField field : fields){
                save(field);
            }
        }
        return fields;
    }


    /**
     * 保存字段设置
     *
     * @param fields 字段列表
     */
    @Override
    public void saveDeptField(List<CrmDeptField> fields) {
        updateBatchById(fields);
    }

    /**
     * 内部调用，保存自定义字段
     *
     * @param crmField 自定义字段
     */
    @Override
    public void saveDeptField(CrmField crmField) {
        List<CrmDeptField> list = lambdaQuery().select(CrmDeptField::getDeptId).eq(CrmDeptField::getLabel, crmField.getLabel()).groupBy(CrmDeptField::getDeptId).list();
        List<CrmDeptField> roleFieldList = new ArrayList<>();
        list.forEach(field -> {
            CrmDeptField roleField = new CrmDeptField();
            roleField.setDeptId(field.getDeptId());
            roleField.setLabel(crmField.getLabel());
            roleField.setFieldId(crmField.getFieldId());
            roleField.setFieldName(crmField.getFieldName());
            roleField.setName(crmField.getName());
            roleField.setAuthLevel(3);
            roleField.setOperateType(1);
            roleField.setFieldType(crmField.getFieldType());
            roleFieldList.add(roleField);
        });
        saveBatch(roleFieldList);
    }

    /**
     * 内部调用，删除自定义字段
     *
     * @param fieldId field
     */
    @Override
    public void deleteDeptField(Integer fieldId) {
        removeByMap(new JSONObject().fluentPut("field_id", fieldId));
    }

    @Override
    public List<Map<Integer, Integer>> queryDeptFieldForGroup(){
        return baseMapper.queryDeptFieldForGroup();
    }
}
